<?php
/**
 * Created by PhpStorm.
 * User: bill
 * Date: 2014/11/23
 * Time: 11:33
 */

include_once 'config.inc.php';
include_once 'tools/tools.common.php';
include_once 'tools/tools.logger.php';
include_once 'linhePDO.php';
include_once  'tools/tools.ErrorStack.php';
include_once  'system.ReturnStack.php';

class Base {

    protected static $tools=null;
    protected static $pdo=null;
    protected static $errStack = null;
    protected static $返回堆栈 = null;
    protected $members;

    //该函数只用于自动测试时使用,平时谨慎使用

    /**
     * 如果子类有构造函数,需要在其中显性调用父类构造函数
     */
    protected function __construct(){


        if (null == self::$tools)
            self::$tools = new Tools();

        if (null == self::$pdo)
            self::$pdo = pdo();

        if (null == self::$errStack)
            self::$errStack = C错误堆栈::getInstance();

        if (null == self::$返回堆栈)
            self::$返回堆栈 = C返回堆栈::getInstance();

    }

    //只serialize非空的

    public function  constructByArray($datas = array()){
        if(sizeof($datas) <= 0) return false;
        $reflectionClass = new ReflectionClass(get_class($this));
        foreach($datas as $k=>$v)
            $reflectionClass->getProperty($k)->setValue($v);
        return true;
    }
    //protected $fdl = 'aa';

    public function __wakeup() {
        $this->members = null;
    }
    public function __sleep() {
        $vars = get_object_vars ($this);
        foreach ($vars as $key => $val) {
            //if($key == 'members') continue;
            if (!empty($val))
                $this->members[$key] = $val;
        }
        return array_keys($this->members);
    }

    /**
     *直接把对象当函数用的时候 $base;
     */
    public function __invoke()
    {
        var_dump($this);
    }

    /**
     * @return string
     */
    public function toString()
    {
        return $this->__toString();
    }

    /**
     * for auto call in    echo $base or (string)$base
     * @return string
     */
    public function __toString()
    {
        return get_class($this);

    }

    /**
     * @return array
     * 返回所有的变量的array（k，v），包括private的,不包括static
     * 在外部调用get_object_vars只返回public的
     *
     */
    public function toArray()
    {
        return get_object_vars($this);
    }

} 